package com.carryking.smart.rpc.discover;

import com.carryking.smart.rpc.constant.DiscoverProviderTypeConstant;
import com.carryking.smart.rpc.discover.impl.ZkDiscoverService;
import com.carryking.smart.rpc.entity.DiscoverConfigEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created by jincarry on 2018/1/11.
 */
public class DiscoverServiceFactory {

    private static DiscoverService discoverService;

    public static synchronized void init(DiscoverConfigEntity configEntity) {
        if (configEntity == null) {
            throw new RuntimeException("初始化服务注册中心失败,服务中心配置信息为空");
        }
        if (StringUtils.isNoneBlank(configEntity.getProviderType())) {
            configEntity.setProviderType(StringUtils.lowerCase(configEntity.getProviderType()));
        }
        if (DiscoverProviderTypeConstant.ZOOKEEPER.equals(configEntity.getProviderType())) {
            discoverService = new ZkDiscoverService(configEntity);
            return;
        }
        throw new RuntimeException("暂不支持除zookeeper外的服务注册和发现,请检查配置");
    }

    public static DiscoverService build() {
        return discoverService;
    }

}
